import time

import requests
import openpyxl
from lxml import html

etree = html.etree
import re
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from xlsxwriter import Workbook
import pandas as pd
import bs4

list4 = []

listUrl = []
imagesUr = []


# chrome.exe --remote-debugging-port=9222


def create_chrome():
    chrom_options = Options()
    # chrom_options.add_argument("--headless")
    # chrom_options.add_experimental_option("prefs",{"profile.managed_default_content_settings.images":2})
    chrom_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
    # chrom_options.add_argument('--disable-gpu')

    path = r'C:\Program Files\Google\Chrome\Application\chrome.exe'
    chrom_options.binary_location = path
    s = Service(r"D:\爬虫项目\pythonProject\PowerTool\chromedriver.exe")
    browser = webdriver.Chrome(service=s, options=chrom_options)
    # browser.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
    #     "source": """
    #               Object.defineProperty(navigator, 'webdriver', {
    #                              get: () => undefined
    #                              })
    #            """
    # })
    return browser


def getUrl():

    browser = create_chrome()
    # for j in range(1,4):
    url1 = f"https://www.bunnings.com.au/products/kitchen/appliances/fridges-freezers/wine-fridges"
    browser.get(url1)
    time.sleep(10)
    shops = browser.find_elements(By.XPATH, '//article')
    for i in shops:
        url = i.find_element(By.XPATH, "../article/div/a").get_attribute('href')
        image = i.find_element(By.XPATH, "./div/a/div/div/div/figure/picture/img").get_attribute('src')
        listUrl.append(url)
        imagesUr.append(image)

def startChrome():
    browser = create_chrome()
    for m,n in zip(listUrl,imagesUr):
        list1 = []
        browser.get(m)
        time.sleep(2)
        title = browser.find_element(By.XPATH, "//h1[@data-locator='product-title']").text
        list1.append(title)
        brand = ""
        list1.append(brand)
        category = ""
        list1.append(category)

        price = browser.find_element(By.XPATH, "//p[@data-locator='product-price']").text
        gallery = ""
        try:
            images = browser.find_elements(By.XPATH, "//div[@class='thumbnailImage']")
            images_1 = []
            for i in images:
                img = i.find_element(By.XPATH, "./img").get_attribute('src')
                images_1.append(img)
            for ims in images_1[1:]:
                gallery = gallery + ims + ";"
        except:
            print("没有二级图片")

        list1.append(n)

        list1.append(gallery)
        color = ""
        size = ""
        list1.append(color)
        list1.append(size)

        PageUrl = m
        sku = PageUrl[-7:]
        list1.append(sku)
        list1.append(price)
        sprice = ""
        list1.append(sprice)
        soup1 = ""
        try:
            desc = browser.find_element(By.XPATH,
                                        "//div[@class = 'MuiGrid-root MuiGrid-item MuiGrid-grid-xs-12 MuiGrid-grid-md-8']")
            html = desc.get_attribute('innerHTML')
            soup = bs4.BeautifulSoup(html)
            for a in soup.findAll('a'):
                del a['href']
            soup1 = soup1 + str(soup)
        except:
            print("没有描述")
        list1.append(soup1)
        list1.append(PageUrl)
        print(list1)
        list4.append(list1)



def create_shopexcel(data: list):
    df = pd.DataFrame(data, columns=["title", "brand", "category", "image", "gallery", "color", "size", "sku", "price",
                                     "sprice", "description", "PageUrl"])
    df.to_excel("shop.xlsx", index=False)


if __name__ == '__main__':
    getUrl()
    startChrome()
    create_shopexcel(list4)
